<?php

namespace frontend\modules\resource\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\HyAudio;
use frontend\modules\common\models\BaseParam;

/**
 * HyAudioSearch represents the model behind the search form of `common\models\HyAudio`.
 */
class HyAudioSearch extends HyAudio
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'res_id', 'cat_id', 'cat_id2', 'is_self'], 'integer'],
            [['start_time', 'end_time'], 'date', 'format' => 'yyyy-mm-dd'],
            [['res_code', 'res_title', 'res_othertitle', 'author', 'author_intro', 'create_date', 'create_adress', 'cl_cat_id', 'keyword', 'summary', 'length', 'album_title', 'album_engtitle', 'resource_id', 'url', 'created_at', 'updated_at', 'deleted_at', 'cover_img'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params, $branchId = 0)
    {
        if ($branchId) {
            $query = HyAudio::find()->joinWith([
                'branchRes' => function ($query) use ($branchId) {
                    $query->andWhere(['hy_branch_resource.br_id' => $branchId]);
                },
                'classes'
            ]);
        } else {
            $query = HyAudio::find()->joinWith(['classes']);
        }

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => BaseParam::$page,
            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
//            'id' => $this->id,
//            'res_id' => $this->res_id,
//            'cat_id' => $this->cat_id,
//            'cat_id2' => $this->cat_id2,
//            'create_date' => $this->create_date,
            'is_self' => $this->is_self,
        ]);

        $query->andFilterWhere(['like', 'res_code', $this->res_code])
            ->andFilterWhere(['like', 'res_title', $this->res_title])
//            ->andFilterWhere(['like', 'res_othertitle', $this->res_othertitle])
            ->andFilterWhere(['like', 'author', $this->author])
//            ->andFilterWhere(['like', 'author_intro', $this->author_intro])
//            ->andFilterWhere(['like', 'create_adress', $this->create_adress])
//            ->andFilterWhere(['like', 'cl_cat_id', $this->cl_cat_id])
            ->andFilterWhere(['like', 'keyword', $this->keyword]);


        if ($this->start_time && $this->end_time) {
            $query->andFilterWhere(['between', 'created_at', $this->start_time, $this->end_time]);
        }

        return $dataProvider;
    }
}
